/*
*********************************************************************************************************
*                                           MASTER INCLUDES
*
* Filename      : includes.h
*********************************************************************************************************
*/

#ifndef  INCLUDES_MODULES_PRESENT
#define  INCLUDES_MODULES_PRESENT

/*
*********************************************************************************************************
*                                         STANDARD LIBRARIES
*********************************************************************************************************
*/

#include  <stdio.h>
#include  <string.h>
#include  <ctype.h>
#include  <stdlib.h>
#include  <stdarg.h>
#include  <math.h>

/*
*********************************************************************************************************
*                                            UCOSIII相关头文件
*********************************************************************************************************
*/

// UCOSIII核心头文件 
typedef signed   char  CPU_CHAR;                        /* 8-bit character                                     */
typedef unsigned char  CPU_BOOLEAN;                     /* Boolean or logical                                  */
typedef unsigned char  CPU_INT08U;                      /* 8-bit unsigned integer                              */
typedef signed   char  CPU_INT08S;                      /* 8-bit   signed integer                              */
typedef unsigned short CPU_INT16U;                      /* 16-bit unsigned integer                             */
typedef signed   short CPU_INT16S;                      /* 16-bit   signed integer                             */
typedef unsigned int   CPU_INT32U;                      /* 32-bit unsigned integer                             */
typedef signed   int   CPU_INT32S;                      /* 32-bit   signed integer                             */
typedef float          CPU_FP32;                        /* Single precision floating point                     */
typedef double         CPU_FP64;                        /* Double precision floating point                     */

typedef unsigned short CPU_STK;                         /* Define size of CPU stack entry                      */
typedef unsigned int   CPU_STK_SIZE;

#define  CPU_SR_ALLOC()

// 附加类型定义 - 移到前面来
typedef unsigned int OS_SEM_CTR;
typedef void (*OS_TASK_PTR)(void *p_arg);
typedef unsigned int OS_PRIO;     // 任务优先级类型
typedef unsigned int OS_MSG_QTY;  // 消息队列数量类型
typedef unsigned int OS_TICK;     // 系统时钟节拍类型
typedef unsigned int OS_OBJ_QTY;  // 对象数量类型
typedef unsigned int OS_OPT;      // 选项类型

// UCOSIII相关结构体定义
// 信号量控制块结构体定义
struct os_sem {
    OS_SEM_CTR   Ctr;                          // 信号量计数器
    CPU_CHAR     Name[16];                     // 信号量名称
};

// 任务控制块结构体定义
struct os_tcb {
    CPU_STK     *StkPtr;                       // 任务堆栈指针
    CPU_STK_SIZE StkSize;                      // 任务堆栈大小
    OS_PRIO      Prio;                         // 任务优先级
    CPU_CHAR     Name[16];                     // 任务名称
};

// UCOSIII相关定义
typedef struct os_sem OS_SEM;
typedef struct os_tcb OS_TCB;
typedef CPU_INT08U  OS_ERR;

// 信号量操作选项
#define OS_OPT_PEND_BLOCKING        0u
#define OS_OPT_PEND_NON_BLOCKING    1u
#define OS_OPT_POST_1               0u

// 任务相关选项
#define OS_OPT_TASK_STK_CHK         (1u << 0u)
#define OS_OPT_TASK_STK_CLR         (1u << 1u)
#define OS_OPT_TASK_SAVE_FP         (1u << 2u)

// 时间延迟选项
#define OS_OPT_TIME_PERIODIC        1u

// 错误代码
#define OS_ERR_NONE                 0u

// 调度器相关
#define DEF_ENABLED                 1u

// 函数声明
void OSInit(OS_ERR *p_err);
void OS_CRITICAL_ENTER(void);
void OS_CRITICAL_EXIT(void);
void OSStart(OS_ERR *p_err);
void CPU_Init(void);
void OSStatTaskCPUUsageInit(OS_ERR *p_err);
void OSSchedRoundRobinCfg(CPU_BOOLEAN en, CPU_INT16U rnd_robin_quad_tick, OS_ERR *p_err);

void OSSemCreate(OS_SEM *p_sem, CPU_CHAR *p_name, OS_SEM_CTR cnt, OS_ERR *p_err);
void OSTaskCreate(OS_TCB *p_tcb, CPU_CHAR *p_name, OS_TASK_PTR p_task, void *p_arg, 
                 OS_PRIO prio, CPU_STK *p_stk_base, CPU_STK_SIZE stk_limit, 
                 CPU_STK_SIZE stk_size, OS_MSG_QTY q_size, OS_TICK time_quanta, 
                 void *p_ext, OS_OPT opt, OS_ERR *p_err);
void OSTaskSuspend(OS_TCB *p_tcb, OS_ERR *p_err);
void OSTaskResume(OS_TCB *p_tcb, OS_ERR *p_err);
void OSTaskDel(OS_TCB *p_tcb, OS_ERR *p_err);
void OSTimeDlyHMSM(CPU_INT08U hours, CPU_INT08U minutes, CPU_INT08U seconds, CPU_INT16U milli, CPU_INT08U opt, OS_ERR *p_err);

OS_ERR OSSemPend(OS_SEM *p_sem, CPU_INT32U timeout, CPU_INT08U opt, void *p_ts, OS_ERR *p_err);
void OSSemPost(OS_SEM *p_sem, CPU_INT08U opt, OS_ERR *p_err);

#endif /* INCLUDES_MODULES_PRESENT */